#include "buzzer.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx.h"
#include "stm32f0xx_misc.h"
#include "USART1.h"

int end_flag = 0;

const uint16_t TONE_TABLE[23] = {50,262,294,330,349,392,440,494,//1--7
																		523,587,659,698,784,880,988,////8--14
																		1046,1175,1318,1397,1568,1760,1967,0};//15--21
//祝你生日快乐
/*const buzzer_mc song0[] = 
{
	{12,2},
	{22,1},	
	{12,1},	
	{13,4},
	{12,4},
	{15,4},
	{14,8},
	{22,4},

	{12,2},
	{22,1},	
	{12,1},	
	{13,4},
	{12,4},
	{16,4},
	{15,8},
	{22,4},

	{12,2},	
	{12,2},	
	{19,4},
	{17,4},
	{15,4},
	{14,4},
	{13,4},
	{22,4},

	{18,4},
	{22,1},
	{18,4},	
	{17,8},
	{13,4},
	{14,8},
	{13,8},	
};
*/

//我在马路边
const buzzer_mc song0[] =
{
	{12,4},
	{15,4},
	{13,2},
	{15,2},
	{12,4},
	
	{10,2},
	{12,2},
	{9,2},
	{10,2},
	{12,4},
	{22,4},
	
	{10,2},
	{12,2},
	{13,2},
	{15,2},
	{12,2},
	{13,2},
	{12,2},
	{10,2},
	{12,2},
	{8,4},
	{10,2},
	{9,4},
	{22,4},
	
	{10,2},
	{9,2},
	{8,2},
	{9,2},
	{10,4},
	{22,4},
	
	{13,2},
	{12,2},
	{10,2},
	{12,2},
	{13,4},
	{22,4},
	
	{12,2},
	{15,2},
	{13,2},
	{12,2},
	{10,2},
	{12,2},
	{9,4},
	
	{12,2},
	{9,2},
	{10,2},
	{9,2},
	{8,4},
	{22,4},
};

const buzzer_mc song1[] = 

{
	{10,4},
	{10,2},
	{10,2},
	{11,4},
	{12,4},
	
	{10,4},
	{22,4},
	{9,4},
	{22,4},
	
	{8,4},
	{8,2},
	{8,2},
	{9,4},
	{10,4},
	{10,6},
	{7,2},
	{7,4},
	{22,4},
	
	{6,4},
	{10,4},
	{9,4},
	{22,4},
	
	{6,4},
	{10,4},
	{9,4},
	{22,4},
	/*
	{6,4},
	{10,4},
	{9,6},
	{8,2},
	{8,4},
	{22,4},
	{22,4},
	{22,4},
	*/
};

const buzzer_mc song2[] = 
{
	{18,2},	
	{14,2},	
	{10,2}, 
	{6,2}, 
};

const buzzer_mc song3[] = 
{
	{6,2},	
	{10,2},	
	{14,2}, 
	{18,2}, 

};

const buzzer_mc song4[] = 
{
	{5,2},	
	{7,2},	
	{8,2}, 
	{7,2}, 

};

const buzzer_mc song5[] = 
{
	
	{15,4},
	{16,4},
	{17,4},
	{15,4},

	{15,4},
	{16,4},
	{17,4},
	{15,4},
	
	{17,4},
	{18,4},
	{19,4},
	{22,4},

	{17,4},
	{18,4},
	{19,4},
	{22,4},
	
	{19,2},
	{20,2},
	{19,2},
	{18,2},
	{17,4},
	{15,4},
	
	{19,2},
	{20,2},
	{19,2},
	{18,2},
	{17,4},
	{15,4},
	
	{16,4},
	{12,4},
	{15,4},
	{22,4},
	
	{16,4},
	{12,4},
	{15,4},
	{22,4},
	
};

const buzzer_mc song6[] = 
{
	{12,2},
	{22,1},	
	{12,1},	
	{13,4},
	{12,4},
	{15,4},
	{14,8},
	{22,4},

	{12,2},
	{22,1},	
	{12,1},	
	{13,4},
	{12,4},
	{16,4},
	{15,8},
	{22,4},
/*
	{12,2},	
	{12,2},	
	{19,4},
	{17,4},
	{15,4},
	{14,4},
	{13,4},
	{22,4},

	{18,4},
	{22,1},
	{18,4},	
	{17,8},
	{13,4},
	{14,8},
	{13,8},	
	*/
};
const buzzer_mc song7[] = 
{
	{2,2},	
	{3,2},	
	{1,2}, 
	{3,2},

};

const buzzer_mc song8[] = 
{
	{8,2},
	{22,2},
	{8,2},

};

const buzzer_mc song9[] = 
{
	{0,4},
	{22,2},
	{0,4},
	{22,2},
	{0,4}, 
};


const buzzer_mc song10[] = 
{
	{9,2},	
	{8,2},	
	{7,2}, 
	{5,2}, 
};

const buzzer_mc song11[] = 
{
	{10,2},	
	{9,2},	
	{8,2}, 
	{9,2}, 

};

const buzzer_mc song12[] = 
{
	{6,2},	
	{10,2},	
	{8,2}, 
	{9,4}, 

};

const buzzer_mc song13[] = 
{
	{15,2},	
	{14,2},	
	{12,2}, 
	{10,2}, 

};

const buzzer_mc song14[] = 
{
	{12,2},	
	{11,2},	
	{10,2}, 
	{11,2}, 

};

const buzzer_mc song15[] = 
{
	{13,2},	
	{10,2},	
	{10,2}, 
	{13,2}, 

};

const buzzer_mc song16[] = 
{
	{3,2},	
	{6,2},	
	{8,2}, 
	{7,2}, 

};

const buzzer_mc song17[] = 
{
	{3,2},	
	{2,2},	
	{5,2}, 
	{1,2}, 

};
const buzzer_mc song18[] = 
{
	{5,2},	
	{6,2},	
	{9,2}, 
	{10,2}, 

};

const buzzer_mc song19[] = 
{
	{7,2},	
	{6,2},	
	{7,2}, 
	{8,2}, 

};

const buzzer_mc song20[] = 
{
	{7,2},	
	{9,1},	
	{9,1},	
	{9,1},	
};

const buzzer_mc song21_ON[] = 
{
	{10,1},	
	{12,1},	
	{14,1},	
	{14,1},	
};

const buzzer_mc song22_OFF[] = 
{
	{14,1},	
	{12,1},	
	{10,1},	
	{10,1},	
};



const buzzer_mc *song_list[COM_MAX]= 
{
	song0,
	song1,
	song2,
	song3,
	song4,
	song5,
	song6,
	song7,
	song8,
	song9,
	song10,
	song11,
	song12,
	song13,
	song14,
	song15,
	song16,
	song17,
	song18,
	song19,
	song20,
	song21_ON,
	song22_OFF,
};

//----------------------------------------------------------------------------	
void BuzzInit(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef	TIM_TimeBaseStructure;
  TIM_OCInitTypeDef	TIM_OCInitStructure;

  /* GPIOA Clocks enable */
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_4);
 
  /* TIM1 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14 , ENABLE);
  
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 48-1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);

  /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OCInitStructure.TIM_Pulse = 0;
  TIM_OC1Init(TIM14, &TIM_OCInitStructure);//PA4

  /* TIM1 counter enable */
  TIM_Cmd(TIM14, ENABLE);

  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM14, ENABLE);

}


uint8_t vol_Buzz_updown = 1;

void Buzzer_volume(uint8_t vol)
{
	if(vol<100)
	{
		vol_Buzz_updown = vol;
	}
}
//--------------------------------------------------------------------------------//
 //放到主函数的初始化中初始化
 /*
 void BuzzInit(void)
{
		NVIC_InitTypeDef NVIC_InitStructure;
     	//定时=36000/72000x2=0.001s=1ms;
		TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
		///////////////////////////////////////////////////////////////
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
		TIM_TimeBaseStructure.TIM_Period = 100-1;//自动重装值（此时改为1ms）
		TIM_TimeBaseStructure.TIM_Prescaler = 72-1;//时钟预分频
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
		TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;        //时钟分频1
		TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;            
		TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);
		TIM_ClearFlag(TIM14,TIM_FLAG_Update);
		TIM_ITConfig(TIM14,TIM_IT_Update,ENABLE);
		TIM_Cmd(TIM14, ENABLE);
		
		NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);
 }
 
void TIM14_IRQHandler(void)
{
    //判断TIM3更新中断是否发生
    if(TIM_GetITStatus(TIM14,TIM_IT_Update)!=RESET)
    {
	        //必须清楚标志位
	    TIM_ClearITPendingBit(TIM14,TIM_IT_Update);
	    //状态取反
	    GPIO_WriteBit(GPIOA,GPIO_Pin_5,(BitAction)(!GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_5)));
    }

}
*/
void set_fre_buzz(uint16_t fre)
{
	uint32_t time_cnt,vol;

	time_cnt = (uint32_t)(1000000/fre);

	TIM_SetAutoreload(TIM14,time_cnt);
	
	vol = vol_Buzz_updown*time_cnt/100;
	TIM_SetCompare1(TIM14,vol);
}

int PlayMusic()
{
	static uint16_t time_cnt = 0;
	static uint16_t play_index = 100;
	static uint16_t beat_cnt = 0;
	uint16_t fre_tone,i;
	static int play_lenth = 4;
	static const buzzer_mc *p_buzzer_play_song = NULL;
	
	time_cnt++;

//	if(time_cnt%125 != 0)
	if(time_cnt%60 != 0)		
	{
		return 1;
	}

	for(i = 0;i < COM_MAX;i++)
	{
		if(com_data[i]==0x55aa)
		{
		play_index = 0;
		beat_cnt = 200;
		com_data[i] = 0;
		Buzzer_volume(3);
		TIM_Cmd(TIM14, ENABLE);
		p_buzzer_play_song = song_list[i];
//		play_lenth = sizeof(song_list[i])/sizeof(buzzer_mc);
			
			if(i==0){
				play_lenth = sizeof(song0)/sizeof(buzzer_mc);
				end_flag = 1;
			}
			else if(i==1){
				play_lenth = sizeof(song1)/sizeof(buzzer_mc);
			}
			else if(i==5){
				play_lenth = sizeof(song5)/sizeof(buzzer_mc);
			}
			else if(i==6){
				play_lenth = sizeof(song6)/sizeof(buzzer_mc);
			}
			else {
				play_lenth=4;
			}
		}
	}
	if(play_index+1>play_lenth)	
	{
		TIM_Cmd(TIM14, DISABLE);
		return 0;
	}

	if(p_buzzer_play_song == NULL)
	{
		return 0;
	}
	beat_cnt++;	
	if(beat_cnt>p_buzzer_play_song[play_index].beat)
	{
		beat_cnt = 1;
		fre_tone = TONE_TABLE[p_buzzer_play_song[play_index].tone];
		set_fre_buzz(fre_tone);
		play_index++;

	}

	return 1;
}

/************************ (C) COPYRIGHT  *****END OF FILE****/
